<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>图书管理</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
</head>
<!--检索组件-->
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">书名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">ISBN</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="st" placeholder="" class="layui-input">
                            <option value="0">有库存</option>
                            <option value="1">已借出</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<!--信息显示表格组件-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>
<!--添加、修改表单组件-->
<div style="display: none" id="bookForm">
    <form class="layui-form" action="">
        <input type="hidden" name="bid">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">书名</label>
                        <div class="layui-input-block">
                            <input type="text" name="booktitle" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">ISBN</label>
                        <div class="layui-input-block">
                            <input type="text" name="bookisbn" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">出版社</label>
                        <div class="layui-input-block">
                            <input type="text" name="bookpublish" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">作者</label>
                        <div class="layui-input-block">
                            <input type="text" name="bookauthor" lay-verify="title" autocomplete="off"
                                   placeholder="请输入标题" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">总数</label>
                        <div class="layui-input-block">
                            <input type="text" name="booknum" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">类别</label>
                        <div class="layui-input-block">
                            <select name="booktype">
                                <option value="1"> 计算机类</option>
                                <option value="2"> 文史类</option>
                                <option value="3"> 社科类</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">位置</label>
                        <div class="layui-input-block">
                            <select id="right" name="bookloc">
                                <option value="1"> 2馆1号架</option>
                                <option value="2"> 2馆2号架</option>
                                <option value="3"> 3馆1号架</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="width: 200px">
                            <p id="demoText"></p>
                        </div>

                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="book-submit">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<!--新增，批量删除组件-->
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        添加新书
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        批量删除
    </button>
</script>
<!--修改删除组件-->
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'upload'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let upload = layui.upload
        let common = layui.common;
        let MODULE_PATH = "operate/";
        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '书名',
                    field: 'bookname',
                    align: 'center'
                },
                {
                    title: '书目图片',
                    field: 'avatar',
                    align: 'center',
                    templet: function (d) {
                        return "<span id=\"pic_" + d.bookId + "\"><img src='../../admin/images/1.webp' style=width:40px lay-event='showPic'></span>";
                    },
                    width: 100
                },
                {
                    title: 'ISBN',
                    field: 'ISBN',
                    align: 'center'
                },
                {
                    title: '出版社',
                    field: 'publish',
                    align: 'center'
                    //templet: '#user-sex'
                },
                {
                    title: '类别',
                    field: 'categary',
                    align: 'center'
                },
                {
                    title: '作者',
                    field: 'wirter',
                    align: 'center'
                },
                {
                    title: '位置信息',
                    field: 'position',
                    align: 'center'
                },
                {
                    title: '总数量',
                    field: 'number',
                    align: 'center'
                },
                {
                    title: '剩余数量',
                    field: 'surplus',
                    align: 'center'
                },

                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center'
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '../../admin/data/book.json',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });
        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: 'https://httpbin.org/post' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……
                $('#demoText').html(''); //置空上传失败的状态
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            //进度条
            , progress: function (n, elem, e) {
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if (n == 100) {
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });


        table.on('tool(user-table)', function (obj) {
            var data = obj.data;
            if (obj.event === "showPic") {
                console.log("data" + data.bookId)
                layer.photos({
                    photos: '#pic_' + data.bookId,
                    shadeClose: false,
                    closeBtn: 2,
                    anim: 0
                });
            };
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function (data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        form.on('switch(user-enable)', function (obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        window.add = function () {
            index = layer.open({
                type: 1,
                title: '新增图书',
                area: ['500px', '600px'],
                content: $('#bookForm')
            });
            form.on('submit(book-submit)', function (data) {
                //校验数据

                //提交数据
                /*$.post(
                        'http://localhost:8081/users/addUser',
                        data.field,
                        function(msg){
                            layer.close(index);//关闭当前页
                            table.reload("user-table");
                        }
                );*/
            });
        }

        window.edit = function (obj) {
            //$.get(totalUrl+'selectOne', {uid:obj.data.uid}, function(obj1){
            index = layer.open({
                type: 1,
                title: '修改图书',
                area: ['500px', '600px'],
                content: $('#bookForm') //注意，如果str是object，那么需要字符拼接。
            });
            //form.val("addForm", obj1);//回显数据
            //form.render('radio');
            //});
            form.on('submit(book-submit)', function (data) {
                //提交数据
                /*$.post(
                        'http://localhost:8081/users/updateUser',
                        data.field,
                        function(msg){
                            console.log('success');
                            layer.close(index);//关闭当前页
                            table.reload("user-table");
                        }
                );*/
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该图书', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['userId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {

            var checkIds = common.checkField(obj, 'userId');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些图书', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
